/*
 *    Copyright 2009-2010 The iBaGuice Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.google.code.ibaguice.samples;

import java.util.List;

import org.apache.ibatis.session.TransactionIsolationLevel;

import com.google.code.ibaguice.dao.AbstractTransactionalDao;
import com.google.code.ibaguice.dao.Transactional;
import com.google.inject.Singleton;

/**
 * 
 * 
 * @author Marco Speranza
 * @version $Id: TransactionalContactDao.java 600 2009-12-29 00:33:36Z simone.tripodi $
 */
@Singleton
abstract public class AbstractTransactionalContactDao extends AbstractTransactionalDao implements ContactDao {

    private final Class<? extends ContactMapper> contactMapperType;

    /**
     * @param contactMapperType
     */
    public AbstractTransactionalContactDao(Class<? extends ContactMapper> contactMapperType) {
        super();
        this.contactMapperType = contactMapperType;
    }

    @Transactional(
            isolationLevel = TransactionIsolationLevel.SERIALIZABLE,
            rethrowExceptionsAs = DaoException.class
    )
    public void insert(final Contact contact) {
        ContactMapper mapper = this.getMapper(this.contactMapperType);
        mapper.add(contact);
    }

    @Transactional(
            isolationLevel = TransactionIsolationLevel.SERIALIZABLE,
            rethrowExceptionsAs = DaoException.class
    )
    public void delete(final Integer id) {
        ContactMapper mapper = this.getMapper(this.contactMapperType);
        mapper.delete(id);
    }

    @Transactional(
            isolationLevel = TransactionIsolationLevel.SERIALIZABLE,
            rethrowExceptionsAs = DaoException.class
    )
    public Contact selectById(final Integer id) {
        ContactMapper mapper = this.getMapper(this.contactMapperType);
        return mapper.getById(id);
    }

    @Transactional(
            isolationLevel = TransactionIsolationLevel.SERIALIZABLE,
            rethrowExceptionsAs = DaoException.class
    )
    public List<Contact> getAll() {
        ContactMapper mapper = this.getMapper(this.contactMapperType);
        return mapper.selectAll();
    }

    @Transactional(
            isolationLevel = TransactionIsolationLevel.SERIALIZABLE,
            rethrowExceptionsAs = DaoException.class
    )
    public void update(final Contact contact) {
        ContactMapper mapper = this.getMapper(this.contactMapperType);
        mapper.update(contact);
    }

}
